package dao;

import bean.getUserInfo.UserInfo;
import common.pojo.Page;
import common.spring.jdbc.MySQLHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.WxUserInfo;
import pojo.form.WxUserInfoAddForm;
import pojo.form.WxUserInfoQueryForm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by asus on 2018/10/19.
 */
@Repository
public class WxUserInfoDao {

    @Autowired
    MySQLHelper mySQLHelper;

    /**
     * 插入用户信息，存在则更新，不存在则插入
     *
     * @param addForm
     * @return
     */
    public int addWxUserInfo(UserInfo addForm) {
        StringBuilder sql = new StringBuilder();
        List list = new ArrayList();
        sql.append("INSERT into wx_user(nickName,avatarUrl,gender,country,province,city,language,openId,parentOpenId,deptId,agentId,createTime) ");
        sql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?) ");
        list.add(addForm.getNickName());
        list.add(addForm.getAvatarUrl());
        list.add(addForm.getGender());
        list.add(addForm.getCountry());
        list.add(addForm.getProvince());
        list.add(addForm.getCity());
        list.add(addForm.getLanguage());
        list.add(addForm.getOpenId());
        list.add(addForm.getParentOpenId());
        list.add(addForm.getDeptId());
        list.add(addForm.getAgentId());
        list.add(new Date());
        sql.append(" on duplicate key UPDATE updateTime= ? ");
        list.add(new Date());
        return mySQLHelper.insert(sql.toString(), list.toArray());
    }

    public WxUserInfo getWxUserInfo(WxUserInfoQueryForm queryForm){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from wx_user where 1=1 ");
        List list = new ArrayList();
        if(StringUtils.isNotBlank(queryForm.getOpenId())){
            sql.append(" and openId = ? ");
            list.add(queryForm.getOpenId());
        }
        return mySQLHelper.queryForBean(sql.toString(),WxUserInfo.class,list.toArray());
    }

    public WxUserInfo getWxUserInfo(Long  id){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from wx_user where 1=1 and id=?  ");
        return mySQLHelper.queryForBean(sql.toString(),WxUserInfo.class,new Object[]{id});
    }


    public WxUserInfo getWxUserInfo(String openId){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from wx_user where 1=1 ");
        sql.append(" and openId = ?");
        return mySQLHelper.queryForBean(sql.toString(),WxUserInfo.class,new Object[]{openId});
    }

    public int updateWxUserInfo(String openId){
        StringBuilder sql =new StringBuilder();
        sql.append(" UPDATE wx_user a left JOIN (select agentId,deptId,openId from wx_user) b ");
        sql.append(" ON a.recommendOpenId = b.openId set a.deptId = b.deptId,a.agentId=b.agentId ");
        sql.append("where a.openId =? ");
        return mySQLHelper.update(sql.toString(),new Object[]{openId});
    }

    public int editWxUserInfo(String openId,Long deptId,Long agentId){
        StringBuilder builder = new StringBuilder();
        builder.append(" update wx_user set agentId=?,deptId=? where openId=? ");
        return mySQLHelper.update(builder.toString(),new Object[]{agentId,deptId,openId});
    }

    public int editWxUserInfo(String openId,String parentOpenId){
        StringBuilder builder = new StringBuilder();
        builder.append(" update wx_user set parentOpenId=? where openId=? ");
        return mySQLHelper.update(builder.toString(),new Object[]{parentOpenId,openId});
    }

    public int editWxUserInfo(String openId,Long agentId){
        StringBuilder builder = new StringBuilder();
        builder.append(" update wx_user set agentId=? where openId=? ");
        return mySQLHelper.update(builder.toString(),new Object[]{agentId,openId});
    }

    public int editWxUserInfoDeptId(String openId,Long deptId){
        StringBuilder builder = new StringBuilder();
        builder.append(" update wx_user set deptId=? where openId=? ");
        return mySQLHelper.update(builder.toString(),new Object[]{deptId,openId});
    }


    public Page<WxUserInfo> getWxUserInfo(WxUserInfoQueryForm queryForm,int curPage,int pageSize){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from wx_user where 1=1 ");
        List list = new ArrayList();
        if(StringUtils.isNotBlank(queryForm.getSearchKey())){
            sql.append(" and nickName like ? ");
            list.add("%"+queryForm.getSearchKey()+"%");
            sql.append(" or phone like ? ");
            list.add("%"+queryForm.getSearchKey()+"%");
        }
        if(queryForm.getDeptId()!=null && queryForm.getDeptId()>0){
            sql.append(" and deptId =? ");
            list.add(queryForm.getDeptId());
        }
        if(queryForm.getAgentId()!=null && queryForm.getAgentId()>0){
            sql.append(" and agentId =? ");
            list.add(queryForm.getAgentId());
        }
        if (StringUtils.isNotBlank(queryForm.getStartTime())) {
            queryForm.setStartTime(queryForm.getStartTime().substring(0, 10) + " 00:00:00");
            sql.append(" and createTime >= ? ");
            list.add(queryForm.getStartTime());
        }
        if (StringUtils.isNotBlank(queryForm.getEndTime())) {
            queryForm.setEndTime(queryForm.getEndTime().substring(0, 10) + " 23:59:59");
            sql.append(" and createTime <= ? ");
            list.add(queryForm.getEndTime());
        }
        sql.append(" order by id desc");
        return mySQLHelper.queryForPageBean(sql.toString(),list.toArray(),WxUserInfo.class,curPage,pageSize);
    }

    public int updatePhone(String phoneNumber,String openId){
        return mySQLHelper.update("update wx_user set phone=? where openId = ? ",new Object[]{phoneNumber,openId});
    }

    public int updateRecommendOpenId(String recommendOpenId,String openId){
        return mySQLHelper.update("update wx_user set recommendOpenId=? where openId = ? ",new Object[]{recommendOpenId,openId});
    }


    public int updateQrcode(String qrcode,String openId){
        return mySQLHelper.update("update wx_user set qrCode=? where openId = ? ",new Object[]{qrcode,openId});
    }

    public int updateMineQrcode(String mineQrCode,String openId){
        return mySQLHelper.update("update wx_user set mineQrCode=? where openId = ? ",new Object[]{mineQrCode,openId});
    }


    public int delete(){
        return mySQLHelper.delete("delete from wx_user where id = 1");
    }
}
